package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.apache.poi.sl.usermodel.MasterSheet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.beans.Transient;
import java.util.List;

@Service
public class DishServicelmpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新境菜品
     * @param dish
     */
    @Override
    public void save(DishDTO dish) {
        Dish di =new Dish();
        BeanUtils.copyProperties(dish,di);
        dishMapper.save(di);
        Long id = di.getId();
        List<DishFlavor> flavors = dish.getFlavors();
        if(flavors!=null&&flavors.size()>0){

            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(id));

            dishFlavorMapper.savebatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pagehapper(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pagehapper(dishPageQueryDTO);
        long total=page.getTotal();
        List<DishVO> result = page.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);
        return pageResult;
    }
    /**
     * 根据菜品id批量删除id
     * @param ids
     */
    @Transactional
    @Override
    public void DeleteById( List<Long> ids) {
        for (Long id : ids) {
          Dish dish= dishMapper.GetByid(id);
          if(dish.getStatus()== StatusConstant.ENABLE)
              throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        List<Long> longs = setmealDishMapper.selectSetmealDishId(ids);
        if(longs!=null&&longs.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

//        for (Long id : ids) {
//            dishMapper.DeleteById(id);
//            dishFlavorMapper.DeleteByid(id);
//        }
        dishMapper.DeleteByIds(ids);
        dishFlavorMapper.DeleteByids(ids);
    }

    /**
     * 根据id回显菜品信息
     * @param id
     * @return
     */

    @Override
    public DishVO FindByIDWithFlaver(Long id) {
      Dish dish= dishMapper.GetByid(id);
      List<DishFlavor>  dishFlavor= dishFlavorMapper.GetByid(id);
      DishVO dishVO=new DishVO();
      BeanUtils.copyProperties(dish,dishVO);
     dishVO.setFlavors(dishFlavor);
      return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO){
       Dish dish =new Dish();
       BeanUtils.copyProperties(dishDTO,dish);
       dishMapper.update(dish);
       dishFlavorMapper.DeleteByid(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.savebatch(flavors);
        }

    }


}
